﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class ThongKeTamUng : UserControl
    {
        public ThongKeTamUng()
        {
            InitializeComponent();
        }

        private void ThongKeTamUng_Load(object sender, EventArgs e)
        {
            loadData();
        }

        void loadData()
        {
            float tongTienTamUng = 0;
            float tongTienTamUngDaThanhToan = 0;
            //float tongTienThanhToan = 0;
            float tongPhieuTamUng = 0;
            float tongPhieuTamUngDaThanhToan = 0;
            //float tongPhieuThanhToan = 0;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            
            var listPhieuTamUng = dc.PhieuTamUngs.Where(
                x => x.QuyTrinhMuaHangs.Single().TrangThai != _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_TAM_UNG &&
                     x.QuyTrinhMuaHangs.Single().TrangThai != _DB.TrangThaiQuyTrinhMuaHang.CHO_DUYET_PHIEU_TAM_UNG &&
                     x.NgayTao >= dateTimePicker1.Value.Date &&
                     x.NgayTao < dateTimePicker2.Value.Date.AddDays(1)
            );
            tongPhieuTamUng = listPhieuTamUng.Count();

            if (tongPhieuTamUng > 0)
            {
                tongTienTamUng = listPhieuTamUng.Sum(x => x.SoTien);
                var listPhieuTamUngDaThanhToan = listPhieuTamUng.Where(
                    x => x.QuyTrinhMuaHangs.Single().TrangThai == _DB.TrangThaiQuyTrinhMuaHang.DA_DUYET_PHIEU_THANH_TOAN ||
                         x.QuyTrinhMuaHangs.Single().TrangThai == _DB.TrangThaiQuyTrinhMuaHang.KET_THUC
                    );
                tongPhieuTamUngDaThanhToan = listPhieuTamUngDaThanhToan.Count();
                tongTienTamUngDaThanhToan = listPhieuTamUngDaThanhToan.Sum(x => x.SoTien);
            }
            

            // show
            txtTienTamUng.Text = tongTienTamUng.ToString("#,#0.##");
            txtTienTamUngChua.Text = (tongTienTamUng - tongTienTamUngDaThanhToan).ToString("#,#0.##");
            txtTienTamUngDa.Text = tongTienTamUngDaThanhToan.ToString("#,#0.##");
            //txtTienThanhToan.Text = tongTienThanhToan.ToString("#,#0.##");
            txtPhieuTamUng.Text = tongPhieuTamUng.ToString("#,#0.##");
            txtPhieuTamUngDa.Text = tongPhieuTamUngDaThanhToan.ToString("#,#0.##");
            txtPhieuTamUngChua.Text = (tongPhieuTamUng - tongPhieuTamUngDaThanhToan).ToString("#,#0.##");
            //txtPhieuThanhToan.Text = tongPhieuThanhToan.ToString("#,#0.##");
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            loadData();
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            loadData();
        }
    }
}
